
/*************************************************************************
*	Version:	V1.0
*	Date	:	2013年09月23日 星期一 16时58分22秒 	 
*	Author:	virlhs@163.com
*	ModuleFile:	ftp.c rabbitmq.c sw_perform.c
*			ftp.h rabbitmq.h sw_perform.h
*	LIB_&_HEADR: amqp.h ampq_framing.h amqp_ssl_socket.h
			amqp_tcp_socket.h
			ftplib.h ftp_var.h
			seft_struct.h seft_util.h seft_errno.h
			json
			libftp.a libjson.a librabbitmq.a
*************************************************************************/
#include <stdlib.h>
#include <stdio.h>
#include "amqp.h"
#include "amqp_framing.h"
#include "amqp_tcp_socket.h"
#include "seft_struct.h"
#include "seft_util.h"
#include "json/json.h"

extern FILE *inforn_lhs;

//#define DEBUG

/******************************************************************************
* brief:	send a message to rabbitMQ server.
* name:		seft_send_message.
* input:	[plate_write] pointer to the sem_writer upload context.
* return:   success or failed.
* notes:
*******************************************************************************/
int seft_send_message(char *message,char *server_ip,char * queue)
{  
      //set flag
	int 	retval = 0;
	//declare the sockfd
	int	status_lhs;

      amqp_socket_t *socket=NULL;

	//set server ip
	const char* hostname=server_ip;
	//set exchange
	const char* exchange="";
	//set the queue
	const char* routingkey=queue ;
	//set message
      //const char* messagebody=plate_write->message;
      char* messagebody=message;
      //set port
	int port = 5672; 

/**********************************************************************************************************/
/*open the socket*/
/**********************************************************************************************************/
    
	amqp_rpc_reply_t 		ret_reply;
	amqp_connection_state_t conn;
	if (0 == retval)
	{   
		conn = amqp_new_connection();
        	
             socket=amqp_tcp_socket_new(conn);
		if(!socket)
		{
		 	printf("fail creating TCP socket\n");
		}
               		
		status_lhs = amqp_socket_open(socket, hostname, port);
		if(status_lhs)
		{
			retval = 1;
             	printf("socket open fail\n");
       		}
		
	}
/**********************************************************************************************************/
/*usr login*/
/**********************************************************************************************************/

	if (0 == retval)
	{   

		ret_reply = amqp_login(conn, "/", 0, 131072, 0, AMQP_SASL_METHOD_PLAIN,"guest", "guest");

		if (AMQP_RESPONSE_NORMAL != ret_reply.reply_type)
		{
			retval = 1;
            		printf("ampq_login fail\n");
		}
 
	}

/**********************************************************************************************************/
/*open the channel*/
/**********************************************************************************************************/
	if (0 == retval)
	{  
		amqp_channel_open(conn, 1);
		ret_reply = amqp_get_rpc_reply(conn);
		if (AMQP_RESPONSE_NORMAL != ret_reply.reply_type)
		{
			retval = 1;
			printf("open channel for rabbitmq fail\n");
		}
       
	}
/**********************************************************************************************************/
/*publish message*/
/**********************************************************************************************************/
	
	if (0 == retval)
	{
		amqp_basic_properties_t props;
		props._flags = AMQP_BASIC_CONTENT_TYPE_FLAG | AMQP_BASIC_DELIVERY_MODE_FLAG;
		props.content_type = amqp_cstring_bytes("text/plain");
		props.delivery_mode = 2; /* persistent delivery mode */
		if (amqp_basic_publish(	conn,
			1,
			amqp_cstring_bytes(exchange),
			amqp_cstring_bytes(routingkey),
			0,
			0,
			&props,
			amqp_cstring_bytes(messagebody)) < 0)
		{
			retval = 1;
			printf("publish the message for rabbitmq fail\n");
		}

	}
/**********************************************************************************************************/
/*close the rabbitMQ*/
/**********************************************************************************************************/
	if (0 == retval)
	{
		ret_reply = amqp_channel_close(conn, 1, AMQP_REPLY_SUCCESS);
		if (AMQP_RESPONSE_NORMAL != ret_reply.reply_type)
		{
			retval = 1;
			printf("amqp_channel_close failed!\n");
		}
	}
	

	if (0 == retval)
	{
		ret_reply = amqp_connection_close(conn, AMQP_REPLY_SUCCESS);
		if (AMQP_RESPONSE_NORMAL != ret_reply.reply_type)
		{
			retval = 1;
			printf("amqp_connection_close failed!\n");
		}
		
	}
	

	if (0 == retval)
	{
		if (amqp_destroy_connection(conn) < 0)
		{
			retval = 1;
			printf("amp_destory_connect erro\n");
		}
		

	//printf("****************************send message over***********************************\n");
	
	
	}
	
	if(retval)
	{
		printf("some thing error occurred in amqp functions\n");
	}

	return (retval);
}


void generate_json_message(p_json_msg p_msg,p_seft_plate_write_t p_write)
{
/******************************/
//plateRoiChilds
char plateRoiX[16] = {0};
char plateRoiY[16] = {0};
char plateRoiW[16] = {0};
char plateRoiH[16] = {0};

sprintf(plateRoiX, "%d", p_msg->roi_plate.x);
sprintf(plateRoiY, "%d", p_msg->roi_plate.y);
sprintf(plateRoiW, "%d", p_msg->roi_plate.width);
sprintf(plateRoiH, "%d", p_msg->roi_plate.height);



json_object *plateRoiChilds=json_object_new_object();


json_object_object_add(plateRoiChilds,"PlateRoiX", json_object_new_string((const char*)plateRoiX));
json_object_object_add(plateRoiChilds,"PlateRoiY", json_object_new_string((const char*)plateRoiY));
json_object_object_add(plateRoiChilds,"PlateRoiW", json_object_new_string((const char*)plateRoiW));
json_object_object_add(plateRoiChilds,"PlateRoiH", json_object_new_string((const char*)plateRoiH));


//-----------------------------//printf("%s\n",json_object_to_json_string(plateRoiChilds));

// colorRgbChilds
char colorRgbB[16] = {0};
char colorRgbG[16] = {0};
char colorRgbR[16] = {0};
sprintf(colorRgbB, "%d", (int)p_msg->color_rgb.val[0]);
sprintf(colorRgbG, "%d", (int)p_msg->color_rgb.val[1]);
sprintf(colorRgbR, "%d", (int)p_msg->color_rgb.val[2]);

json_object *colorRgbChilds=json_object_new_object();
json_object_object_add(colorRgbChilds,"ColorRgbB", json_object_new_string((const char*)colorRgbB));
json_object_object_add(colorRgbChilds,"ColorRgbG", json_object_new_string((const char*)colorRgbG));
json_object_object_add(colorRgbChilds,"ColorRgbR", json_object_new_string((const char*)colorRgbR));
//-----------------------------//printf("%s\n",json_object_to_json_string(colorRgbChilds));

// colorAreaChilds
char colorAreaX[16] = {0};
char colorAreaY[16] = {0};
char colorAreaW[16] = {0};
char colorAreaH[16] = {0};
sprintf(colorAreaX, "%d", p_msg->color_area.x);
sprintf(colorAreaY, "%d", p_msg->color_area.y);
sprintf(colorAreaW, "%d", p_msg->color_area.width);
sprintf(colorAreaH, "%d", p_msg->color_area.height);

json_object *colorAreaChilds=json_object_new_object();
json_object_object_add(colorAreaChilds,"ColorAreaX", json_object_new_string((const char*)colorAreaX));
json_object_object_add(colorAreaChilds,"ColorAreaY", json_object_new_string((const char*)colorAreaY));
json_object_object_add(colorAreaChilds,"ColorAreaW", json_object_new_string((const char*)colorAreaW));
json_object_object_add(colorAreaChilds,"ColorAreaH", json_object_new_string((const char*)colorAreaH));
//-----------------------------//printf("%s\n",json_object_to_json_string(colorAreaChilds));


//eventImgFeatureChilds
char plateRotationAngle[128] = {0};
sprintf(plateRotationAngle, "%lf", p_msg->plate_rotation_angle);

json_object *eventImgFeatureChilds=json_object_new_object();
json_object_object_add(eventImgFeatureChilds,"EventColorLayout", json_object_new_string((const char*)p_msg->eventColorLayout));
json_object_object_add(eventImgFeatureChilds,"EventColorStructure", json_object_new_string((const char*)p_msg->eventColorStructure));
json_object_object_add(eventImgFeatureChilds,"EventEdgeHistogram", json_object_new_string((const char*)p_msg->eventEdgeHistogram));
json_object_object_add(eventImgFeatureChilds,"EventDominantColor", json_object_new_string((const char*)p_msg->eventDominantColor));
json_object_object_add(eventImgFeatureChilds,"EventScalableColor", json_object_new_string((const char*)p_msg->eventScalableColor));
json_object_object_add(eventImgFeatureChilds,"PlateRotationAngle", json_object_new_string((const char*)plateRotationAngle));
json_object_object_add(eventImgFeatureChilds,"PlateRoi", plateRoiChilds);
json_object_object_add(eventImgFeatureChilds,"ColorRgb", colorRgbChilds);
json_object_object_add(eventImgFeatureChilds,"ColorArea",colorAreaChilds);
//-----------------------------//printf("%s\n",json_object_to_json_string(eventImgFeatureChilds));
//json_object_put(plateRoiChilds);
//json_object_put(colorRgbChilds);
//json_object_put(colorAreaChilds);


// eventChilds
char roadDirection[16] = {0};
char roadWay[16] = {0};
char target[16] = {0};
char targetAction[16] = {0};
char targetColor[16] = {0};
char targetSpeed[16] = {0};
sprintf(roadDirection, "%u", p_msg->roadDirection);
sprintf(roadWay, "%u", p_msg->roadWay);
sprintf(target, "%u", p_msg->target);
sprintf(targetAction, "%u", p_msg->targetAction);
sprintf(targetColor, "%u", p_msg->targetColor);
sprintf(targetSpeed, "%u", p_msg->targetSpeed);

json_object *eventChilds=json_object_new_object();
json_object_object_add(eventChilds,"PanoramaName", json_object_new_string((const char*)p_msg->panoramaName));
json_object_object_add(eventChilds,"RoadDirection", json_object_new_string((const char*)roadDirection));
json_object_object_add(eventChilds,"RoadWay", json_object_new_string((const char*)roadWay));
json_object_object_add(eventChilds,"Target", json_object_new_string((const char*)target));
json_object_object_add(eventChilds,"TargetAction", json_object_new_string((const char*)targetAction));
json_object_object_add(eventChilds,"TargetColor", json_object_new_string((const char*)targetColor));
json_object_object_add(eventChilds,"TargetSpeed", json_object_new_string((const char*)targetSpeed));
json_object_object_add(eventChilds,"eventImgFeature",eventImgFeatureChilds);
//-----------------------------//printf("%s\n",json_object_to_json_string(eventChilds));
//json_object_put(eventImgFeatureChilds);

//imgFeatureChilds
json_object *imgFeatureChilds=json_object_new_object();
json_object_object_add(imgFeatureChilds,"ColorLayout",json_object_new_string((const char*)p_msg->colorLayout));
json_object_object_add(imgFeatureChilds,"ColorStructure", json_object_new_string((const char*)p_msg->colorStructure));
json_object_object_add(imgFeatureChilds,"EdgeHistogram", json_object_new_string((const char*)p_msg->edgeHistogram));
json_object_object_add(imgFeatureChilds,"DominantColor", json_object_new_string((const char*)p_msg->dominantColor));
json_object_object_add(imgFeatureChilds,"ScalableColor", json_object_new_string((const char*)p_msg->scalableColor));
//-----------------------------//printf("%s\n",json_object_to_json_string(imgFeatureChilds));

//json_object_put(imgFeatureChilds);
//vehicleChilds
char plateNumberStructure[16] = {0};
char plateNumberColor[16] = {0};
char logo[16] = {0};
char color[16] = {0};
char size[16] = {0};
char type[16] = {0};
char speed[16] = {0};
char direction[16] = {0};
char hasSunShield[16] = {0};
char hasSeatBelt[16] = {0};
sprintf(plateNumberStructure, "%u", p_msg->plateNumberStructure);
sprintf(plateNumberColor, "%u", p_msg->plateNumberColor);
sprintf(logo, "%u", p_msg->logo);
sprintf(color, "%u", p_msg->color);
sprintf(size, "%u", p_msg->size);
sprintf(type, "%u", p_msg->type);
sprintf(speed, "%u", p_msg->speed);
sprintf(direction, "%u", p_msg->direction);
sprintf(hasSunShield, "%u", p_msg->has_sun_shield);
sprintf(hasSeatBelt, "%u", p_msg->has_seat_belt);

json_object *vehicleChilds=json_object_new_object();
json_object_object_add(vehicleChilds,"PlateNumber", json_object_new_string((const char*)p_msg->plateNumber));
json_object_object_add(vehicleChilds,"PlateNumberStructure", json_object_new_string((const char*)plateNumberStructure));
json_object_object_add(vehicleChilds,"PlateNumberColor", json_object_new_string((const char*)plateNumberColor));
json_object_object_add(vehicleChilds,"Logo", json_object_new_string((const char*)logo));
json_object_object_add(vehicleChilds,"Color", json_object_new_string((const char*)color));
json_object_object_add(vehicleChilds,"Size", json_object_new_string((const char*)size));
json_object_object_add(vehicleChilds,"Type", json_object_new_string((const char*)type));
json_object_object_add(vehicleChilds,"Speed", json_object_new_string((const char*)speed));
json_object_object_add(vehicleChilds,"Direction", json_object_new_string((const char*)direction));
json_object_object_add(vehicleChilds,"VehiclePicName", json_object_new_string((const char*)p_msg->vehiclePicName));
json_object_object_add(vehicleChilds,"ImgFeature", imgFeatureChilds);
json_object_object_add(vehicleChilds,"seatbelt", json_object_new_string((const char*)hasSeatBelt));
json_object_object_add(vehicleChilds,"sunshield", json_object_new_string((const char*)hasSunShield));

//-----------------------------//printf("%s\n",json_object_to_json_string(vehicleChilds));



//rootChilds
char warningType[16] = {0};
sprintf(warningType, "%u", p_msg->warningType);
//string ternimalID = seft_get_ftpserver_username();
uint8_t terminalID[128]={0};
strcpy(terminalID,p_msg->terminalID);

//printf("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++terminalID%s\n",terminalID);
//string relativePath = seft_get_ftpserver_username();


/*
relativePath += "/";

relativePath += p_write->ftp_ctx.ftp_curr_dir;
relativePath += "/";
relativePath += p_write->ftp_ctx.ttl_name;
*/

json_object *rootChilds=json_object_new_object();
json_object_object_add(rootChilds,"EventId", json_object_new_string((const char*)p_msg->eventID));
json_object_object_add(rootChilds,"ShotedTime", json_object_new_string((const char*)p_msg->shortedTime));
json_object_object_add(rootChilds,"TerminalID", json_object_new_string((const char*)terminalID));
json_object_object_add(rootChilds,"RelativePath", json_object_new_string((const char*)p_msg->relativePath));
json_object_object_add(rootChilds,"WarningType", json_object_new_string((const char*)warningType));
json_object_object_add(rootChilds,"ExternalID", json_object_new_string((const char*)p_msg->panoramaName));
json_object_object_add(rootChilds,"Vehicle",vehicleChilds);
json_object_object_add(rootChilds,"Event",eventChilds);
//-----------------------------//printf("%s\n",json_object_to_json_string(rootChilds));
sprintf((char *)(p_write->message), "%s" ,json_object_to_json_string(rootChilds));

//printf("%s\n" ,json_object_to_json_string(rootChilds));
fprintf(inforn_lhs,"%s\n",json_object_to_json_string(rootChilds));

json_object_put(plateRoiChilds);
json_object_put(colorRgbChilds);
json_object_put(colorAreaChilds);
json_object_put(eventImgFeatureChilds);
json_object_put(eventChilds);
json_object_put(imgFeatureChilds);
json_object_put(vehicleChilds);
json_object_put(rootChilds);

     #ifdef DEBUG
	printf("%s\n" ,"message generate over");
	#endif
fprintf(inforn_lhs,"%s\n" ,"message generate over");
}




/*
int main()
{
   seft_plate_write_t put_number;
   char *serverip="192.168.1.54";
    char *queue="myqueque"; 
   seft_send_message(&put_number,serverip,queue);
return 0;
}
*/

